<?php

/*
 * this api
 */

/**
 * Description of api
 *
 * @author seven
 */
class Api extends Http_Controller{
    /*
     * 我们在构造函数里 创建了一些最基本需求操作
     *
     */
	public function __construct()
	{
		parent::__construct();
		$this->load->library('socket_client');
		$this->load->library('authenticate');

	}

	public function validateCode()
	{
		$this->load->library('validateCode');
		$this->validatecode->doimg();
		$this->session->set_userdata(array('validateCode'=> $this->validatecode->getCode()));
	}

	public function validateCode_tnf()
	{
		$validateCode_post = $this->input->post('validateCode');
		$validateCode = $this->session->userdata('validateCode');
		if($validateCode_post != $validateCode)
		{
			$data['type'] = false;
			$data['data'] = '验证码不一致';	
			$data['url'] = '';
		}
		else
		{
			$data['type'] = true;
			$data['data'] = '验证码正确';	
			$data['url'] = '';
		}
		
		if($this->input->is_ajax_request())
		{
				echo json_encode($data);
		}
		else
		{
				$this->load->v('common/ts',$data);
		}
	}
	  /*
	   * 这里是短信接口 
	   * 这里必须有四个参数  第一个app_key 第二个secret_key: 密匙 第三个sms_number:发送的号码 第四个sms:短信内容
	   *
	   */
	    public function sms()
	    {
			$app_key = $this->input->post('app_key');
			$secret_key = $this->input->post('secret_key');
			$number = $this->input->post('sms_number');
			$sms = $this->input->post('sms');
			$authenticate = $this->authenticate->init($app_key,$secret_key,'sms');
			if($authenticate === 1)
			{
				$return['type'] = true;
				$this->load->config('sms',true);
				if(preg_match('"/13[0-9]{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|5|6|7|8|9]\d{8}/"', $number) && $sms)
				{
					$this->load->library('socket_client');
					$this->socket_client->set_address($this->CI->config->item('sms_service_port','sms'));
					$this->socket_client->set_port($this->CI->config->item('sms_service_ip','sms'));
					$this->socket_client->set_socket_message(json_encode(array('sms'=>$sms,'number'=>$number)));
					$data = $this->socket_client->init();
					echo json_encode(array('type'=>true,'msg'=>'服务器返回:'.$data));
					return;
				}
				else
				{
					echo json_encode(array('type'=>false,'msg'=>'提交的内容有误'));
					return;
				}
			}
			else if($authenticate === 0)
			{
				$return['type'] = false;
				$return['data'] = 'sercet_key错误';
			}
			else if($authenticate === -1)
			{
				$return['type'] = false;
				$return['data'] = 'appkey格式不正确';
			}
			else if($authenticate === -2)
			{
				$return['type'] = false;
				$return['data'] = 'sercet_key格式不正确';
			}
			else if($authenticate === -3)
			{
				$return['type'] = false;
				$return['data'] = '用户不存在';
			}
	    }
}
